package cn.controller;

import cn.config.C2_01_SignHelloWorld;
import cn.config.Result;
import cn.config.WaterMarkUtil;
import cn.domain.Employee;
import cn.domain.FileInfo;
import cn.service.EmployeeService;
import cn.service.FileInfoService;
import com.bw.itext.pdf.service.PdfService;
import com.bw.springboot.starter.upload.model.Chunk;
import com.bw.springboot.starter.upload.service.FileService;

import com.bw.springboot.starter.upload.util.FileResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itextpdf.text.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
public class ListController {
@Autowired
    EmployeeService employeeService;
@Autowired
    FileService fileService;
@Autowired
    FileInfoService fileInfoService;
@Autowired
    PdfService pdfService;
@Autowired
    C2_01_SignHelloWorld c201SignHelloWorld;
    @RequestMapping("create")
    public  String create() throws Exception {
        List<Employee> list = employeeService.lists();
         pdfService.export(list,"Employee.pdf","员工详情",Employee.class);
        c201SignHelloWorld.create("D:\\pdf\\sign\\Employee.pdf","D:\\pdf\\sign\\EmployeeSign.pdf","D:\\pdf\\sign\\sign.png");
        WaterMarkUtil.addWaterMark("D:\\pdf\\sign\\EmployeeSign.pdf","D:\\pdf\\sign\\EmployeeSignWater.pdf","月考必升班",3);
    return  "success";
    }
    @RequestMapping("preview")
    public  String preview(HttpServletResponse response) throws Exception {
        pdfService.preview("EmployeeSignWater.pdf",response);
        return  "success";
    }
    @RequestMapping("download")
    public  String download(HttpServletResponse response) throws Exception {
        pdfService.download("EmployeeSignWater.pdf",response);
        return  "success";
    }

@RequestMapping("upload")
    public  Object upload(Chunk chunk) throws InterruptedException {
     fileService.postFileUpload(chunk);
     if (chunk.getTotalChunks()==chunk.getChunkNumber()){
         TimeUnit.SECONDS.sleep(2);
         FileResult fileResult = fileService.mergeFile(chunk);
         FileInfo fileInfo = new FileInfo();
         fileInfo.setFilename(chunk.getFilename());
         fileInfo.setStartTime(new Date());
         fileInfo.setRelativepath("d:/path/"+chunk.getRelativePath());
          fileInfoService.save(fileInfo);


         return  fileResult;

     }
             System.out.println("上传成功");
        return "sucess";
}

    @RequestMapping("list")
    public Result list(@RequestBody Employee employee,Integer pageNum) {
        PageHelper.startPage(pageNum,3);
        List<Employee> list = employeeService.list(employee);
        PageInfo<Employee> info = new PageInfo<>(list);
        return  Result.success(info);
    }
    @RequestMapping("add")
    public Result add(@RequestBody Employee employee) {

        boolean save = employeeService.save(employee);
      return  save?Result.success():Result.fail();
    }

}
